package com.leetcode.no841;
import java.util.*;

public class Solution {
    boolean[] vis;
    int num;

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        //Vector<Integer> visited = new Vector<Integer>(rooms.size(), 0);
        // visited.indexOf(0) = 1;
        int n = rooms.size();
        num = 0;
        vis = new boolean[n];
        dfs(rooms, 0);
        return num == n;
    }

    public void dfs(List<List<Integer>> rooms, int x){
        vis[x] = true;
        num ++;

        for(int it: rooms.get(x)){
            if(!vis[it]){
                dfs(rooms, it);
            }
        }
    }
}

class Solution01{
    public boolean canVisitAllRooms(List<List<Integer>> rooms){
        int n = rooms.size(), num = 0;
        boolean[] vis = new boolean[n];

        Queue<Integer> que = new LinkedList<Integer>();

        vis[0] = true;
        que.offer(0);
        while(!que.isEmpty()){
            int x = que.poll();
            num++;
            for(int it:rooms.get(x)){
                if(!vis[it]){
                    vis[it] = true;
                    que.offer(it);
                }
            }
        }
        return num == n;
    }
}